package pers.vic.boot.console.test.i18n;

/**
 * 描述:
 * @author Vic.xu
 * @date 2021-12-10 10:33
 */

import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.tool.Tools;
import pers.vic.boot.console.util.I18nUtil;

import javax.annotation.Resource;
import java.util.Locale;

@RestController
@RequestMapping("/test/i18n")
public class I18nController {

    @Resource
    public MessageSource messageSource;

    @RequestMapping("/name")
    public BaseResponse name() {
        String code = "test.name";
        String name = I18nUtil.get(code);
        return BaseResponse.success(name);
    }

    @RequestMapping("/local")
    public BaseResponse local() {
        String code = "test.name";
        I18nUtil.switchOnce(Locale.US);
        String name = "us: " + I18nUtil.get(code);
        I18nUtil.switchOnce(Locale.CHINA);
        name += " zh:" + I18nUtil.get(code);
        return BaseResponse.success(name);
    }

    @RequestMapping("/switch")
    public BaseResponse switchLocale(int flag) {
        Locale locale = Locale.CHINA;
        if (flag == 0) {
            locale = Locale.US;
        }
        I18nUtil.switchLocale(Tools.currentRequest(), Tools.currentResponse(), locale);
        return BaseResponse.success();
    }

    public static void main(String[] args) {
        System.out.println(Locale.US.toLanguageTag());
    }

}
